using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class PlayerTank : TankBase
{
    public WeaponObj weaponObj;
    public Transform weaponMount;

    // Update is called once per frame
    void Update()
    {
        //前后
        this.transform.Translate(Input.GetAxis("Vertical") * Vector3.forward * Time.deltaTime * moveSpeed);

        //旋转
        this.transform.Rotate(Vector3.up, Input.GetAxis("Horizontal") * Time.deltaTime * roundSpeed);

        //炮台旋转
        turretObject.Rotate(Vector3.up, Input.GetAxis("Mouse X") * Time.deltaTime * HeadRoundSpeed);

        //开火
        if (Input.GetMouseButtonDown(0))
        {
            Fire();
        }
    }

    public override void Fire()
    {
        if (weaponObj == null)
        {
            return;
        }
        weaponObj.Fire();
    }

    public override void Hurt(TankBase other)
    {
        base.Hurt(other);
        GamePanel.Instance.UpdateHP(hp);
    }

    /// <summary>
    /// 切换武器
    /// </summary>
    /// <param name="obj"></param>
    public void ChangeWeapon(GameObject obj)
    {
        if (weaponObj != null)
        {
            Destroy(weaponObj.gameObject);
            weaponObj = null;
        }

        GameObject weapon = Instantiate(obj, weaponMount, false);
        weapon.transform.SetParent(weaponMount);
        weaponObj = weapon.GetComponent<WeaponObj>();
        weaponObj.SetFather(this);
    }

    public override void DestroyTank()
    {
        Time.timeScale = 0;
        FailPanel.Instance.ShowMe();
    }
}